package com.yoomiito.app.present.my.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.yoomiito.app.Constant;
import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.model.bean.BankBean;
import com.yoomiito.app.model.bean.BingPhoneBean;
import com.yoomiito.app.model.my.MyCollectGoodsBean;
import com.yoomiito.app.model.my.MyUserInfo;
import com.yoomiito.app.net.UploadFileManager;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.my.MyUserInfoActivity;
import com.yoomiito.app.ui.my.RealNameActivity;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.YMToast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import retrofit2.http.Field;

public class RealNamePresenter extends BaseActPresenter<RealNameActivity> {

    private Disposable mDisposable;
    public RealNamePresenter(AppConfig appConfig) {
        super(appConfig);
    }
    /**
     * 销毁倒计时任务
     */
    public void destroyCountdownTask() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
    }
    /**
     * 开始倒计时
     */
    public void startCountdownTask() {
        mDisposable = Flowable.intervalRange(0, 45, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> getV().CountdownTimeTask((int) (45 - aLong)))
                .doOnComplete(() -> getV().timeEndTask()).subscribe();
    }

    //发送验证码
    public void requestSendCode(String phone) {
        YmApi.getYmService().sendCode(phone,"86","1")
                .compose(getV().bindToLifecycle())
                .compose(XApi.<BingPhoneBean>getApiTransformerData())
                .compose(XApi.<BingPhoneBean>getScheduler())
                .subscribe(new ApiSubscriber<BingPhoneBean>(handleException()) {
                    @Override
                    public void onSuccess(BingPhoneBean bingPhoneBean) {
                        getV().showToast("发送成功");
                        startCountdownTask();
                        getV().dismissLoading();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (error.getCode()==2003){
                            getV().showToast("发送失败");
                        }
                        getV().dismissLoading();
                    }
                });
    }

    public void getBankMessage() {
        getV().showLoading();
        YmApi.getYmService()
                .getBankMessage()
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BankBean>(handleException()) {
                    @Override
                    public void onSuccess(BankBean data) {
                        if (data != null) {
                            getV().setData(data);
                        }
                        getV().dismissLoading();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                    }
                });
    }

    public void insertBank(String province,
                           String provinceCode,
                           String city,
                           String cityCode,
                           int bankId,
                           String branch,
                           String accountNumber,
                           String name,
                           String mobile,
                           String idCard,
                           String code) {
        getV().showLoading();
        YmApi.getYmService()
                .insertBank(province, provinceCode, city, cityCode, bankId, accountNumber, name, mobile, idCard, branch, code)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel data) {
                        getV().dismissLoading();
                        YMToast.showText("添加成功");
                        getV().finish();
                    }
                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                    }
                });
    }


    public void updateBank(String id,
                           String province,
                           String provinceCode,
                           String city,
                           String cityCode,
                           int bankId,
                           String branch,
                           String accountNumber,
                           String name,
                           String mobile,
                           String idCard,
                           String code) {
        getV().showLoading();
        YmApi.getYmService()
                .updateBank(id,province, provinceCode, city, cityCode, bankId, accountNumber, name, mobile, idCard, branch, code)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel data) {
                        YMToast.showText("修改成功");
                        getV().dismissLoading();
                        getV().finish();

                    }
                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                    }
                });
    }
}
